package com.zhz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.domain.UserAddress;
import com.zhz.model.R;
import com.zhz.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author ：zhz
 * @date ：Created in 2021/01/02
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@RestController
@RequestMapping("/userAddress")
@Api(tags = "用户钱包地址")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;

    @GetMapping
    @ApiOperation(value = "查用户钱包地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户的 Id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    public R<Page<UserAddress>> findByPage(@ApiIgnore Page<UserAddress> page, Long userId) {
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(userAddressService.findByPage(page, userId));
    }

    @GetMapping("/current")
    @ApiOperation(value = "获取当前用户的钱包地址")
    public R getCurrentUserAddress(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        List<UserAddress> userAddressList = userAddressService.getUserAddressByUserId(Long.valueOf(userId));
        return R.ok(userAddressList);
    }
    @GetMapping("/getCoinAddress/{coinId}")
    @ApiOperation(value = "查询用户某种币的钱包地址")
    public R<String> getCoinAddress(@PathVariable("coinId") Long coinId){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        UserAddress userAddress = userAddressService.getUserAddressByUserIdAndCoinId(userId,coinId);
        return R.ok(userAddress.getAddress());
    }
}
